package com.mbuswidget;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MBusWidgetConfigurationSettings extends Activity {
    private int widgetID;
    SharedPreferences prefs;
    ToggleButton useLocationToggle;
    EditText maxArrivalTime;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.widgetconfigsettings);
        
        
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
		widgetID = getIntent().getIntExtra(
				AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);
		
		if(widgetID == AppWidgetManager.INVALID_APPWIDGET_ID)
			finish();
		
		Button blacklistBtn = (Button)findViewById(R.id.blacklistBtn);
		blacklistBtn.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MBusWidgetConfigurationSettings.this, MbusWidgetConfigurationSettingsBlacklisted.class);
				intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID); 
				startActivityForResult(intent, 0);
			}
		});
		
		useLocationToggle = (ToggleButton) findViewById(R.id.useLocationToggle);
		useLocationToggle.setChecked(MBusWidgetConstants.GetUseLocation(prefs, widgetID));
		useLocationToggle.setOnClickListener(new OnClickListener() {
		    public void onClick(View v) {
		        // Perform action on clicks
		        MBusWidgetConstants.SetUseLocation(prefs, widgetID, useLocationToggle.isChecked());
		        
		        // TODO: Remove Toast
//		        Toast.makeText(MBusWidgetConfigurationSettings.this, "Use Location is " + (MBusWidgetConstants.GetUseLocation(prefs, widgetID) ? "on" : "off"), Toast.LENGTH_SHORT).show();
		    }
		});
		
		maxArrivalTime = (EditText)findViewById(R.id.maxArrivalTimes);
		maxArrivalTime.setText(MBusWidgetConstants.GetMaxArrivalTimes(prefs, widgetID).toString());
		maxArrivalTime.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				try {
					int value = Integer.parseInt(s.toString());
					MBusWidgetConstants.SetMaxArrivalTimes(prefs, widgetID, value);
				} catch (Exception e) {

				}
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
			}
		});
		
		Button serviceConfig = (Button)findViewById(R.id.launchServiceConfigBtn);
		serviceConfig.setOnClickListener(new OnClickListener() {
		    public void onClick(View v) {
		        // Perform action on clicks
		        Intent intent = new Intent(MBusWidgetConfigurationSettings.this, MBusWidgetServiceConfig.class);
		        startActivity(intent);
		        
		        // TODO: Remove Toast
//		        Toast.makeText(getApplicationContext(), "Auto Update is " + (MBusWidgetConstants.GetDownloadIntervalEnabled(prefs) ? "on" : "off"), Toast.LENGTH_SHORT).show();
		    }
		});
    }

}
